<template>
  <!-- 这是积分兑换页面 -->
  <div id="integral">
    <el-container>
      <el-header><Header></Header></el-header>
      <el-main>
        <div class="main_frame">
          <h3>积分兑换</h3>
          <div class="personal_points">
            <div class="personal_integral">
              个人积分：<span>{{ userInfo.credit1 || "0" }}分</span>
            </div>
            <div class="personal_records" @click="record">
              <i class="el-icon-tickets"></i> 参与记录
            </div>
          </div>
          <div class="ulNavigationBar">
            <ul class="ulBar">
              <li
                v-for="item in navbar"
                :key="item.id"
                @click="ulBar(item.id)"
                :class="[ulBarNum == item.id ? 'licolor' : '']"
              >
                {{ item.name }}
              </li>
            </ul>
          </div>
          <!-- 积分商品 -->
          <div class="integralGoods">
            <ul class="g_row_ul">
              <li
                v-for="(data, index) in renderingArr"
                :key="data.id"
                @click="goods(renderingArr[index].id)"
              >
                <img :src="url + data.thumb" alt="" />
                <h4>{{ data.title }}</h4>
                <div class="exchange">
                  <span class="integralNum">{{ data.credit }}</span
                  >积分
                  <span v-show="data.money > 0" class="integralNum"
                    >+￥{{ data.money }}</span
                  >
                  <button>兑换</button>
                </div>
              </li>
            </ul>
          </div>
          <!-- 分页 -->
          <div class="pagination" v-show="pagBool">
            <el-pagination
              background
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-size="pageSize"
              layout="total, prev, pager, next, jumper"
              :total="total"
            >
            </el-pagination>
          </div>
        </div>
      </el-main>
      <el-footer><Footer></Footer></el-footer>
    </el-container>
  </div>
</template>

<script>
import Header from "@/components/Header.vue";
import Footer from "@/components/Footer.vue";
export default {
  data() {
    return {
      userInfo: {
        nickname: "", // 名称
        headimgurl: "", // 头像
        credit1: "" //积分
      },
      ulBarNum: "", // 默认颜色
      renderingArr: [], // 渲染积分商品，数组
      navbar: [], //兑换商品导航栏
      url: "https://mp.uekea.cn/attachment/",
      // 传给分页组件的数据
      currentPage: 1, //默认第一页
      pageSize: 6, //每页显示多少条数据
      total: 0, //一共多少条数据
      pagBool: true //分页的显示隐藏
    };
  },
  components: {
    Header,
    Footer
  },
  methods: {
    // 积分兑换导航栏
    ulBar(i) {
      var _this = this;
      _this.ulBarNum = i;
      // _this.pageList(i + 1);
      _this.$get(`integral/category/goods/${i}`).then(res => {
        if (res.code == 200) {
          _this.renderingArr = res.data;
          if (res.total > 6) {
            _this.total = res.total;
            _this.pagBool = true;
          } else {
            _this.pagBool = false;
          }
        } else {
          _this.$MessageAlert(true, _this.$t("share.Message.error"), "error");
        }
      });
    },
    // 积分商品详情
    goods(id) {
      this.$router.push({
        path: "/integralGoods",
        query: { integralGoodsID: id }
      });
    },
    // 分页子组件传的值
    child(i) {
      this.renderArray(i);
    },
    // 参与记录
    record() {
      this.$router.push({
        path: "/Record",
        query: { integralGoodsID: 40}
      });
      
    },
    // 分页监听当前所在页数
    handleCurrentChange(val) {
      var _this = this;
      _this.$get(`integral/list/${val}`).then(res => {
        if (res.code == 200) {
          _this.renderingArr = res.data;
          if (res.total > 6) {
            _this.total = res.total;
            _this.pagBool = true;
          } else {
            _this.pagBool = false;
          }
        } else if (res.code == 404) {
          _this.$MessageAlert(true, _this.$t("share.Message.error"), "error");
        }
      });
    }
  },
  created() {
    var _this = this;
    // 请求用户数据
    let id = _this.$Cookie.getCookie("userID");
    if (_this.$Cookie.getCookie("token") != undefined) {
      _this.$get(`user/info/${id}`).then(res => {
        if (res.code == 200) {
          _this.userInfo = res.data;
          _this.imageUrl = res.data.avatar;
        }
      });
    } else {
      // 没token值
      _this.$MessageAlert(true, _this.$t("share.Message.login"), "error");
      _this.$router.push({ path: "/" });
    }

    //请求导航栏
    _this.$get(`integral/category`).then(res => {
      if (res.code == 200) {
        _this.navbar = res.data;
      } else if (res.code == 404) {
        _this.$MessageAlert(true, _this.$t("share.Message.error"), "error");
      }
    });

    // 请求全部商品
    _this.$get("integral/list").then(res => {
      if (res.code == 200) {
        _this.renderingArr = res.data;
        if (res.total > 6) {
          _this.total = res.total;
          _this.pagBool = true;
        } else {
          _this.pagBool = false;
        }
      } else {
        _this.$MessageAlert(true, _this.$t("share.Message.error"), "error");
      }
    });
  }
};
</script>

<style lang="less" scoped>
// 布局容器
.el-header {
  font-size: 0;
}
.el-header,
.el-footer {
  color: #333;
  text-align: center;
  height: auto !important;
  padding: 0px;
}
.el-main {
  color: #333;
  padding: 0px;
  // 计算减去头部和底部的高度，就是中间的高度
  min-height: calc(100vh - 61px - 220px);
}
.el-aside {
  color: #333;
  text-align: center;
}
// 布局容器结束
#integral {
  min-width: 1190px;
}
.main_frame {
  width: 950px;
  // border: 1px solid red;
  margin: auto;
  margin-top: 50px;
  h3 {
    margin-bottom: 20px;
  }
  .personal_points {
    font-size: 16px;
    border: 1px solid #ccc;
    padding: 8px;
    margin-bottom: 30px;
    .personal_integral {
      display: inline-block;
      span {
        display: inline-block;
        // width: 70px;
        // border: 1px solid yellow;
      }
    }
    .personal_records {
      display: inline-block;
      margin-left: 50px;
      cursor: pointer;
      .el-icon-tickets {
        vertical-align: middle;
        margin-bottom: 2px;
        font-size: 20px;
      }
    }
  }
  .pagination {
    width: 100%;
    margin-top: 35px;
    margin-bottom: 45px;
    text-align: center;
  }
  .ulNavigationBar {
    height: 26px;
    // border:1px solid red;
  }
  .ulBar {
    li {
      font-size: 15px;
      float: left;
      padding: 2px 20px;
      border-right: 1px solid #ccc;
      cursor: pointer;
      // border: 1px solid grey;
    }
    li:last-child {
      border-right: 0px;
    }
    .licolor {
      color: rgb(215, 176, 108);
    }
  }
}
.g_row_ul {
  width: 100%;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  text-align: center;
  li {
    // border-radius: 10px;
    width: 26%;
    margin-top: 40px;
    transition-duration: 1s;
    img {
      height: 250px;
      width: 100%;
    }
    h4 {
      font-size: 17px;
      height: 40px;
      line-height: 40px;
      // padding-top: 10px;
      // padding-left: 15px;
      // padding-right: 15px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      color: black;
      margin: 0px;
      // margin: 10px 0px;
    }
    .exchange {
      font-size: 12px;
      margin-bottom: 10px;
      padding-left: 20px;
      text-align: left;
      .integralNum {
        color: #e2b972;
        display: inline-block;
        // margin-left: 30px;
        margin-right: 5px;
        font-size: 15px;
        // clear: both;
      }
      .integralMoney {
        display: inline-block;
      }
      button {
        font-size: 15px;
        float: right;
        padding: 0px 5px;
        margin-right: 30px;
        background-color: #999;
        color: #fff;
        margin-right: 5px;
        border-radius: 4px;
      }
    }
  }
}
.g_row_ul::after {
  content: "";
  display: block;
  width: 26%;
}
.g_row_ul li:hover {
  transform: translateY(-10px);
  transition-duration: 1.5s;
  box-shadow: 0px 8px 12px 3px #4e5454;
}
</style>